#ifndef SUBJECT_LIB_H_
#define SUBJECT_LIB_H_

#include "Subject.h"
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>

void Add_Subject(string s, Subject l[], int &numberSubject){
   string temp, a_id, a_sname, a_stype, a_presub;
   int a_credit;

   int mark = s.find(":");
   a_id = s.substr(0,mark);


   temp = s.substr(mark+1);
   mark = temp.find(":");
   a_sname = temp.substr(0,mark);

   temp = temp.substr(mark+1);
   mark = temp.find(":");
   a_stype = temp.substr(0,mark);

   temp = temp.substr(mark+1);
   mark = temp.find(":");
   a_credit = atoi(temp.substr(0,mark).c_str());

   temp = temp.substr(mark+1);
   mark = temp.find(":");
   a_presub = temp.substr(0,mark);

   Subject Sub(a_id, a_sname, a_stype, a_credit, a_presub);
   l[numberSubject] = Sub;
   numberSubject = numberSubject + 1;
}
void ReadSubject(string _file, Subject l[], int &numberSubject){
               ifstream fin;
               fin.open(_file.c_str());
               if(!fin.is_open()){
                       cout<<"Cannot open file.\n";
                       exit(1);
               }else{
                              string s = "";
                              while (getline(fin, s)){
                                Add_Subject(s,l,numberSubject);
                              }
                              fin.close();
               }
}

void WriteSubject(string _file, Subject l[], int &numberSubject){
               ofstream fout;
fout.open(_file.c_str());
               for (int i=0 ; i<numberSubject ; i++){
                              fout<<l[i].getId()<<":"<<l[i].getName()<<":"<<l[i].getType()<<":"<<l[i].getCredit()<<":"<<l[i].getPresub()<<":"<<endl;
               }
               fout.close();
}

void Search_by_Presub(string key, string _file, int &k){
     Subject b_l[100];
     int i=0,j=0;
     ReadSubject(_file,b_l,i);

     for (j = 0; j < i; j++){
         if (b_l[j].getPresub().find(key) != std::string::npos){
            b_l[j].Show();
            k++;
         }
     }
}

string GetSubjectNameByID(string s_id, string _file){
    Subject l[100];
    int i = 0, j=0;
    ReadSubject(_file, l, i);

    string _name = "";

    for (j=0; j < i; j++){
        if (l[j].getId() == s_id){
            _name = l[j].getName();
        }
    }

    return _name;
}

short GetCreditsByID(string s_id, string _file){
    Subject l[100];
    int i = 0, j=0;
    ReadSubject(_file, l, i);

    int _credit = 0;

    for (j=0; j < i; j++){
        if (l[j].getId() == s_id){
            _credit = l[j].getCredit();
        }
    }

    return _credit;
}

string GetPresubByID(string s_id, string _file){
    Subject l[100];
    int i = 0, j=0;
    ReadSubject(_file, l, i);

    string _pre = "";

    for (j=0; j < i; j++){
        if (l[j].getId() == s_id){
            _pre = l[j].getPresub();
        }
    }

    return _pre;
}
#endif

